﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Infrastructure;
using Microsoft.Practices.Prism.Commands;
using PluginManager;
using DBDataAccessLib;
using DBDataAccessLib.DataMapping;
using PlayInfo.View;
using UserControlLib;
using System.IO;

namespace PlayInfo.ViewModel
{
    public class PlayInfoViewModel : ViewModelBase
    {
        private IScan scan;
        private PlayInfoWindow window;
        public PlayInfoViewModel()
        {
            SaveCommand = new DelegateCommand<object>(OnSaveCommand, OnCanSaveCommand);
            string portOption = Helper.GetConfigurationValue("ScanOptions");
            window = new PlayInfoWindow();
            if (portOption == "PortScan") scan = new SerialPortScan(Helper.GetConfigurationValue("PortNumber"), 100);
            else scan = new RFIDScan();
            scan.Initial();
            scan.OnDataReceived += new Action<string>(scan_OnDataReceived);
        }

        public override void OnRemoved()
        {
            scan.Close();
            window.CloseWindow();
        }

        void scan_OnDataReceived(string sn)
        {
            WCNO = sn.Substring(0, 3);
            Sebango = sn.Substring(3, 4);
            Model = sn.Substring(7, 3);
            StationName = sn.Substring(10, 4);
            OnSaveCommand(null);
        }

        public void OnSaveCommand(object obj)
        {
            this.IsBusy = true;
            MessageContent = "正在查询数据...";
            ThreadPool.QueueUserWorkItem(new WaitCallback(currentContext =>
            {
                SynchronizationContext context = currentContext as SynchronizationContext;
                MachineInfoRecordMapping record = MachineInfoDataAccess.GetMachineInfoByPrimKey(WCNO, StationName, Model, 1);
                context.Send(new SendOrPostCallback(p =>
                {
                    IsBusy = false;
                    MachineInfoRecordMapping r = p as MachineInfoRecordMapping;
                    if (r == null)
                    {
                        ModalWindowService.ShowMessage("没有对应的记录。");
                        return;
                    }
                    if (string.IsNullOrWhiteSpace(r.Essentials))
                    {
                        ModalWindowService.ShowMessage("没有可以显示的图片。");
                        return;
                    }
                    if (Helper.GetConfigurationValue("Client") != "Office" && r.WCNO != Helper.GetConfigurationValue("WCNO"))
                    {
                        ModalWindowService.ShowMessage("流水线号和当前流水线号不一致。");
                        return;
                    }
                    if (Helper.GetConfigurationValue("Client") != "Office" && r.StationName != Helper.GetConfigurationValue("StationName"))
                    {
                        ModalWindowService.ShowMessage("工位名称和当前工位名不一致。");
                        return;
                    }
                    string[] paths = r.Essentials.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    string baseUri = "";
                    if (Helper.GetConfigurationValue("Client") == "Office") baseUri = Helper.ServerUrl;
                    else baseUri = Helper.BaseUri;
                    if (!Directory.Exists(baseUri + r.WCNO + "\\" + r.StationName))
                    {
                        ModalWindowService.ShowMessage("没有需要显示的图片。");
                        return;
                    }
                    string[] files = Directory.GetFiles(baseUri + r.WCNO + "\\" + r.StationName);
                    foreach (var pa in paths)
                    {
                        bool fileExist = false;
                        foreach (var f in files)
                        {
                            FileInfo info = new FileInfo(f);
                            if (info.Name == pa)
                            {
                                fileExist = true;
                                break;
                            }
                        }
                        if (!fileExist)
                        {
                            ModalWindowService.ShowMessage("需要播放的文件：" + pa + "不存在。");
                            return;
                        }
                    }

                    window.Record = r;
                    window.Reset();
                    window.Show();
                    window.Activate();
                }), record);
            }), SynchronizationContext.Current);

        }

        public bool OnCanSaveCommand(object obj)
        {
            return !string.IsNullOrWhiteSpace(WCNO) && !string.IsNullOrWhiteSpace(Sebango) && !string.IsNullOrWhiteSpace(Model) &&
                !string.IsNullOrWhiteSpace(StationName);
        }

        public DelegateCommand<object> SaveCommand { get; set; }

        private string wcno;

        public string WCNO
        {
            get { return wcno; }
            set
            {
                wcno = value.Trim();
                OnPropertyChanged("WCNO");
                SaveCommand.RaiseCanExecuteChanged();
            }
        }

        private string stationName;

        public string StationName
        {
            get { return stationName; }
            set
            {
                stationName = value.Trim();
                OnPropertyChanged("StationName");
                SaveCommand.RaiseCanExecuteChanged();
            }
        }

        private string model;

        public string Model
        {
            get { return model; }
            set
            {
                model = value.Trim();
                OnPropertyChanged("Model");
                SaveCommand.RaiseCanExecuteChanged();
            }
        }

        private string sebango;

        public string Sebango
        {
            get { return sebango; }
            set
            {
                sebango = value.Trim();
                OnPropertyChanged("Sebango");
                SaveCommand.RaiseCanExecuteChanged();
            }
        }
    }
}
